import axios from 'axios'
import {
  Notify
} from 'vant'
import router from '../router'
import store from '@/store'
import qs from 'qs'

// const baseUrl = store.state.apiUrl
let baseUrl = store.state.baseUrl;

// switch (process.env.NODE_ENV) {
//   case "development": //生产环境
//     // baseUrl = "/api";
//     // baseUrl = "http://192.168.128.91";
//     baseUrl = "https://wxtest.tofocus.cn";
//     // baseUrl = "http://192.168.2.109:23700";
//     break;
//   case "test": //测试环境
//     baseUrl = "https://wxtest.tofocus.cn";
//     break;
//   default:
//     baseUrl = "https://wxtest.tofocus.cn";
// }
const rightCode = '00000' //正确返回code
const noLoginCode = 999 //未登录返回code
const failCode = 401 //token失效code
function isLogin() { //未登录
  return
  window.localStorage.removeItem('userList')
  if (router.history.current.path != '/') {
    Notify({
      message: '登录超时',
      type: 'danger'
    })
    router.replace({
      path: '/?role=merchant'
    })
  }
}

function isToken() { //
  return
  window.localStorage.removeItem('userList')
  Notify({
    message: '登录超时',
    type: 'danger'
  })
  console.log('登录超时')
  location.reload()

}
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 30000,
  // withCredentials: true
})
const instance2 = axios.create({
  baseURL: baseUrl,
  timeout: 30000,
  withCredentials: false,
  headers: {
    "Content-Type": 'application/json'
  }
})
const instance3 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  responseType: "blob",
  headers: {
    "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
  }
})
const instance4 = axios.create({
  baseURL: baseUrl,
  timeout: 300000,
  responseType: "blob",
  headers: {
    "Content-Type": 'application/json'
  }
})

function changeLoading(bl) { //开关加载动画
  // store.state.loadingShow = bl
}



instance.interceptors.request.use(config => {
  try {
    var openid = store.state.openid ? store.state.openid : ''


    if (openid) {
      config.headers.common['openid'] = openid;
    }
    var appToken = store.state.appToken ? store.state.appToken : ''


    if (appToken) {
      config.headers.common['Authorization'] = appToken;
    }
  } catch (e) {
    throw e
  }
  return config
}, err => {
  changeLoading(false) //关闭加载动画
  Notify({

    message: '请求超时',
    type: 'danger'
  })
  return Promise.reject(err);
})
instance2.interceptors.request.use(config => {
  try {
    var openid = store.state.openid ? store.state.openid : ''
    if (openid) {
      config.headers.common['openid'] = openid;
    }
    var appToken = store.state.appToken ? store.state.appToken : ''


    if (appToken) {
      config.headers.common['Authorization'] = appToken;
    }
  } catch (e) {
    throw e
  }
  return config
}, )
instance3.interceptors.request.use(config => {
  try {
    var openid = store.state.openid ? store.state.openid : ''
    if (openid) {
      config.headers.common['openid'] = openid;
    }
    var appToken = store.state.appToken ? store.state.appToken : ''


    if (appToken) {
      config.headers.common['Authorization'] = appToken;
    }
  } catch (e) {
    throw e
  }
  return config
})

instance.interceptors.response.use(
  response => {
    changeLoading(false) //关闭加载动画
    if (response.data instanceof Blob) {
      return Promise.resolve(response)
    }
    console.log(response.data.status, rightCode)
    if (response.data.status == rightCode) { //返回值正确返回值
      return response.data
    } else if (response.data.status == failCode) {

      isToken()
      return response.data
    } else if (response.data.status == noLoginCode) {

      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      Notify({

        message: response.data.message,
        type: 'danger'
      })
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    console.log('err', error)
    return Promise.reject(error)
  }
)
instance2.interceptors.response.use(
  response => {
    changeLoading(false) //关闭加载动画
    if (response.data.status == rightCode) { //返回值正确返回值
      return response.data
    } else if (response.data.status == failCode) {

      isToken()
      return response.data
    } else if (response.data.status == noLoginCode) {

      isLogin()
      return response.data
    } else { //如果报错  提示错误信息
      Notify({

        message: response.data.message,
        type: 'danger'
      })
      return response.data
    }
  },
  error => {
    changeLoading(false) //关闭加载动画
    console.log('err', error)
    return Promise.reject(error)
  }
)

export default {
  baseUrl,
  get(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .get(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  put(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .put(url, param)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  delete(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance
        .delete(url, {
          params: param,
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  post(
    url,
    param,
    resParams = {
      notify: true
    },
    data = {
      loading: false
    }, //是否开启loading
  ) {
    if (data.loading == true) { //post接口传入{loading:true}
      changeLoading(true)
    }
    return new Promise((resolve, reject) => {
      instance
        .post(url, qs.stringify(param), {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //对象提交
  JSON_Post(
    url,
    param,
    resParams = {
      notify: true
    },
    // baseUrl = baseUrl,
  ) {
    changeLoading(true)
    return new Promise((resolve, reject) => {
      instance2
        .post(url, param, {
          resParams: resParams
        })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出表单格式
  blob_get(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance3
        .get(url, qs.stringify(param)
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出表单格式
  blob_post(
    url,
    param,
    resParams = {
      notify: true
    }
  ) {
    return new Promise((resolve, reject) => {
      instance3
        .post(url, qs.stringify(param)
          //    {
          //   params: param,
          //   resParams: resParams
          // }
        )
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  //导出JSON格式 数组传递
  blob_post_json(
    url,
    param,
    resParams = {
      notify: true
    },
    // baseUrl = baseUrl,
  ) {
    changeLoading(true)
    return new Promise((resolve, reject) => {
      instance4
        .post(url, param, resParams)
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}